package com.lagou.job02;

import com.lagou.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 测试类s
 */
public class JDBC_PrepareStatement {

    @Test
    public void testInser() throws SQLException {
        Connection con = JDBCUtils.getConnection();
        PreparedStatement ps = con.prepareStatement("INSERT INTO employee VALUES (?,?,?,?,?,?)");
        ps.setObject(1, null);
        ps.setString(2, "孙悟空");
        ps.setString(3,"男");
        ps.setDouble(4, 10000);
        ps.setDouble(5, 20000);
        ps.setString(6, "1993-04-03");
        int i = ps.executeUpdate();
        System.out.println("插入了" + i + "条数据");

        ps.setObject(1, null);
        ps.setString(2, "猪八戒");
        ps.setString(3,"男");
        ps.setDouble(4, 1000);
        ps.setDouble(5, 2000);
        ps.setString(6, "1994-04-03");
        i = ps.executeUpdate();
        System.out.println("插入了" + i + "条数据");

        JDBCUtils.close(con, ps);
    }

    @Test
    public void testQuery() throws SQLException {
        Connection con = JDBCUtils.getConnection();

        PreparedStatement ps = con.prepareStatement("select * from employee");
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            System.out.println("name = " + name + ", gender = " + gender);
        }

        System.out.println("-----------");

        ps = con.prepareStatement("select  * from employee where name = ?");
        ps.setString(1,"孙悟空");
        ResultSet resultSet1 = ps.executeQuery();
        while (resultSet1.next()) {
            String name = resultSet1.getString("name");
            String gender = resultSet1.getString("gender");
            System.out.println("name = " + name + ", gender = " + gender);
        }

        JDBCUtils.close(con, ps);
    }
}
